from datetime import date, timedelta

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.users.models import User


class UserCountView(APIView):
  def get(self, request):
    now_date = date.today()
    # count = User.objects.filter(date_joined__lte=now_date).count()
    count = User.objects.all().count()
    return Response({'count': count, 'date': now_date})


class DayActiveView(APIView):
  # permission_classes = [IsAdminUser]

  def get(self, request):
    now_data = date.today()
    count = User.objects.filter(last_login__gte=now_data).count()

    return Response({'count': count, 'date': now_data})


class DayIncrementView(APIView):
  def get(self, request):
    now_data = date.today()
    count = User.objects.filter(date_joined__gte=now_data).count()

    return Response({'count': count, 'date': now_data})


class DayOrdersView(APIView):



  def get(self, request):
    now_data = date.today()
    count = User.objects.filter(orderinfo__create_time__gte=now_data).count()

    return Response({'count': count, 'date': now_data})


class MonthIncrementView(APIView):
  def get(self, request):
    now_day = date.today()
    old_day = now_day - timedelta(days=30)

    date_lits = []
    for i in range(30):
      start_day = old_day + timedelta(days=i)
      end_day = old_day + timedelta(days=i + 1)
      count = User.objects.filter(date_joined__gte=start_day, date_joined__lte=end_day).count()
      date_lits.append({
        'count': count,
        'date': start_day
      })
    return Response(date_lits)
